
auto_modules ()
{
    aliases="$(find /sys/devices/ -name modalias -exec cat {} +)"
    mods=""
    for a in $aliases; do
        m="$(resolve-modalias "/lib/modules/${KERNELVERSION}/modules.alias" "$a")"
        [ -n "$m" ] && mods="$mods $m"
    done

    echo "${mods}" | sed 's|-|_|g'
    [ -z "${mods}" ] && return 1
    return 0
}

all_modules ()
{
    mods=$(find ${MODULEDIR} -name '*.ko' 2>/dev/null | grep $@ | sort -u)

    echo "${mods}"
    [ -z "${mods}" ] && return 1
    return 0
}

checked_modules ()
{
    if [ -e "${MODULE_FILE}" ]; then
        for mod in $(all_modules $@); do
            modname="$(basename ${mod%%\.ko} | sed 's|-|_|g')"
            if grep "^${modname}$" "${MODULE_FILE}" >/dev/null 2>&1; then
                echo ${modname}
            fi
        done
        return 1
    else
        all_modules ${@}
    fi
}

msg () { [ "${QUIET}" = "n" ] && echo $@; }
err () { echo "ERROR: $@" >&2; }
die () { echo "FATAL: $@" >&2; cleanup; exit 1; }

add_full_dir ()
{
    if [ -n "${1}" -a -d "${1}" ]; then
        for f in ${1}/*; do
            if [ -d "${f}" ]; then
                add_full_dir "${f}"
            else
                add_file "${f}"
            fi
        done
    fi
}

add_dir ()
{
    #skip root directory and "." for relative directories... i.e. /foo/bar/./blah
    if [ -n "${1}" -a "${1}" != "/" -a "${1}" != "." ]; then
        if ! grep "dir ${1} " "${FILELIST}" 2>&1 > /dev/null; then
            add_dir $(dirname "${1}")
            msg "  adding  dir ${1}"
            echo "dir ${1} 755 0 0" >> "${FILELIST}"
        fi
    fi
}

# add_device /dev/foo type major minor [permissions]
add_device ()
{
    if [ $# -ge 4 ]; then
        local perms
        perms="${5:-644}"
        if ! grep "nod ${1}" "${FILELIST}" 2>&1 > /dev/null; then
            add_dir $(dirname "${1}")
            msg "  adding node ${1}"
            echo "nod ${1} ${perms} 0 0 ${2} ${3} ${4}" >> "${FILELIST}"
        fi
    else
        err "invalid device node format: $@"
        return 1
    fi
}

# what the hell does this do?
add_symlink ()
{
    local fil dest dir
    if [ -L ${1} ]; then
        fil="${1##$BASEDIR}"
        dest="${2##$BASEDIR}"
        add_dir $(dirname "${dest}")
        add_dir $(dirname "${fil}")
        if ! grep "slink ${dest} " "${FILELIST}" 2>&1 > /dev/null; then
            msg "  adding link ${fil} -> ${dest}"
            echo "slink ${fil} ${dest} $(stat -c '%a' ${1}) 0 0" >> "${FILELIST}"
        fi
    fi
    #fail quietly
}

add_symlink2 ()
{
    add_dir $(dirname ${1})
    add_dir $(dirname ${2})
    if ! grep -F -q "slink ${1} " "${FILELIST}" 2>&1 >/dev/null; then
         msg "  adding link ${1} -> ${2}"
         echo "slink ${1} ${2} 777 0 0" >> "${FILELIST}"
    fi
}

add_file ()
{
    local fil lnk dir dest
    if [ -f "${1}" ]; then
        fil="${1}"
        lnk=$(readlink -f "${fil}")
        if [ -n "${lnk}" ]; then
            add_symlink "${fil}" "${lnk}"
            fil="${lnk}"
        fi
        if [ $# -eq 2 ]; then
            dest="${2}"
        else
            dest="${fil##$BASEDIR}"
            if [ "${dest:0:1}" != "/" ]; then
                dest="/${dest}"
            fi
        fi

        add_dir $(dirname "${dest}")

        if ! grep "file ${dest} " "${FILELIST}" 2>&1 > /dev/null; then
            msg "  adding file ${dest}"
            echo "file ${dest} ${fil} $(stat -c '%a' ${fil}) 0 0" >> "${FILELIST}"
        fi
    else
        err "file '${1}' does not exist"
        return 1
    fi
}

HAS_MODULES="n"
#modules are handled specially in order to enable autodetection
add_module ()
{
    local fil path fw mod deps
    #cleanup - remove .ko, replace - and _ with [-_] to match either
    fil=$(basename "${1}" | sed -e "s|[-_]|\[-_\]|g" -e "s|\.ko$||g")

    found=0
    for path in $(find "${MODULEDIR}" -type f -name "${fil}.ko"); do
        #get needed firmware files
        for fw in $(/sbin/modinfo -F firmware "${path}"); do
            [ -f "/lib/firmware/$fw" ] && add_file "/lib/firmware/$fw"
        done
        #get module depends
        for mod in $(/sbin/modinfo -F depends "${path}" | tr ',' ' '); do
            if [ -n "${mod}" ]; then
                add_module "${mod}"
            fi
        done
        HAS_MODULES="y"
        add_file "${path}" && found=1
    done
    if [ ${found} -eq 0 ]; then
        err "module '$fil' not found"
    fi
}

add_binary ()
{
    local bin dest type lib
    bin=$(which "${1}")
    if [ $? -ne 0 ]; then
        bin="${1}"
    fi

    dest=""
    if [ $# -eq 2 ]; then
        dest=${2}
    fi

    if [ ! -f "${bin}" ]; then
        err "'${bin}' is not a file"
        return 1
    fi

    if [ $? -eq 0 ]; then
        type=$(file -b "${bin}")
        case "${type}" in
            *script*)
            msg "   adding '${type}' script, ensure proper interp exists..."
            add_file "${bin}" ${dest}
            ;;
            *executable*|*shared\ object*|*symbolic\ link*)
            add_file "${bin}" ${dest}
            #note, this will also handle 'not a dynamic executable' spit out by
            # static binaries... the deps will produce nothing
            for lib in $(ldd ${bin} 2>/dev/null | sed "s|.*=>\(.*\)|\1|"); do
                if [ -n "${lib}" ]; then
                    #remove TLS libraries
                    notls=$(echo ${lib} | sed 's|/lib/tls.*/\(lib.*\)|/lib/\1|')
                    [ -e "${notls}" ] && lib="${notls}"
                    [ -f "${lib}" ] && add_file "${lib}"
                fi
            done
            ;;
            *)
            err "unknown type '${type}' for binary '${bin}'"
            return 1
            ;;
        esac
    fi
}

parse_hook ()
{
    local mod bin fil
    for mod in ${MODULES}; do
        if [ -n "${mod}" ]; then
            add_module "${mod}"
        fi
    done

    for bin in ${BINARIES}; do
        if [ -n "${bin}" ]; then
            add_binary "${bin}"
        fi
    done

    for fil in ${FILES}; do
        if [ -n "${fil}" ]; then
            add_file "${fil}"
        fi
    done

    if [ -n "${SCRIPT}" ]; then
        add_file "${HOOKDIR}/${SCRIPT}" "/hooks/${SCRIPT}"
    fi
}
# vim: set ft=sh ts=4 sw=4 et:
